package com.lc.cms.service.impl;

import com.lc.cms.dao.IPermissionDao;
import com.lc.cms.dto.PagingResponseDto;
import com.lc.cms.dto.PermissionDto;
import com.lc.cms.dto.PermissionPagingParameterDto;
import com.lc.cms.model.PermissionModel;
import com.lc.cms.service.IPermissionService;
import com.lc.cms.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Log4j2
@Service
public class PermissionServiceImpl implements IPermissionService {

    private final IPermissionDao permissionDao;

    @Autowired
    public PermissionServiceImpl(IPermissionDao permissionDao) {
        this.permissionDao = permissionDao;
    }

    @Override
    public ResponseResult create(PermissionModel permission) {
        try {
            int createResult = permissionDao.create(permission);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(PermissionModel permission) {
        try {
            int updateResult = permissionDao.update(permission);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult delete(Integer permissionId) {
        try {
            int deleteRolePermissionResult = permissionDao.deleteRolePermissionByPermissionId(permissionId);
            if (deleteRolePermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteDepartmentPermissionResult = permissionDao.deleteDepartmentPermissionByPermissionId(permissionId);
            if (deleteDepartmentPermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteResult = permissionDao.delete(permissionId);
            if (deleteResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult getListAll() {
        try {
            List<PermissionDto> list = permissionDao.getListAll();
            return ResponseResult.success(list);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult getListPagingByCondition(PermissionPagingParameterDto permission) {
        try {
            int amount = permissionDao.getCountByCondition(permission);
            List<PermissionDto> list = permissionDao.getListPagingByCondition(permission);
            PagingResponseDto<PermissionDto> userResponse = new PagingResponseDto<>();
            userResponse.setAmount(amount);
            userResponse.setList(list);
            return ResponseResult.success(userResponse);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
